package com.huixuebao.exchange.service.Impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.utils.StringUtils;
import com.huixuebao.common.bean.ApiResult;
import com.huixuebao.common.enumeration.ResultCodeEnum;
import com.huixuebao.exchange.entity.BaseDivision;
import com.huixuebao.exchange.mapper.BaseDivisionMapper;
import com.huixuebao.exchange.service.BaseDivisionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class BaseDivisionServiceImpl implements BaseDivisionService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    BaseDivisionMapper baseDivisionMapper;


    @Override
    public ApiResult<List<BaseDivision>> getAreaTree() {
        ApiResult apiResult = new ApiResult();
        String trees = redisTemplate.opsForValue().get("list");
        List<BaseDivision> redistree = JSONObject.parseArray(trees, BaseDivision.class);
        if (StringUtils.isEmpty(trees)) {
            List<BaseDivision> regions = baseDivisionMapper.regionList();
            List<BaseDivision> tree = new ArrayList<>();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (Iterator<BaseDivision> it = regions.iterator(); it.hasNext(); ) {
                BaseDivision region = (BaseDivision) it.next();
                if ("86".equals(region.getPId())) {
                    recursionFn(regions, region);//递归列表
                    region.getCreateDate();
                    tree.add(region);
                }
            }
            String jsonString = JSON.toJSON(tree).toString();
            redisTemplate.opsForValue().set("list", jsonString);
            apiResult.setData(tree);
            apiResult.setSuccess(true);
            apiResult.setCode(ResultCodeEnum.SUCCESS.getCode());
            apiResult.setMessage("获取地区码成功，走数据库读");
            return apiResult;
        } else {
            apiResult.setData(redistree);
            apiResult.setSuccess(true);
            apiResult.setCode(ResultCodeEnum.SUCCESS.getCode());
            apiResult.setMessage("获取地区码成功，从redis取出");
            return apiResult;
        }
    }



    private void recursionFn(List<BaseDivision> list, BaseDivision region){
        // 得到子节点列表
        List<BaseDivision> childList = getChildList(list, region);
        region.setChildren(childList);
        for(BaseDivision child : childList){
            if(hasChild(list,child)){
                //判断是否有子节点
                Iterator<BaseDivision> it = childList.iterator();
                while (it.hasNext()){
                    BaseDivision n = (BaseDivision) it.next();
                    recursionFn(list, n);
                }
            }
        }
    }private List<BaseDivision> getChildList(List<BaseDivision> list, BaseDivision region){
        List<BaseDivision> tlist = new ArrayList<BaseDivision>();
        Iterator<BaseDivision> it = list.iterator();
        while (it.hasNext()){
            BaseDivision region1 = (BaseDivision) it.next();
            if(region1.getPId().equals(region.getId())){
                tlist.add(region1);
            }
        }
        return tlist;
    }
    private boolean hasChild(List<BaseDivision> list, BaseDivision baseDivision){
        return getChildList(list,baseDivision).size() > 0 ? true : false;
    }
}
